home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 February / enter-2006-02.iso / files / httrack-3.40.exe / {app} / src / htsname.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-01-21  |  49.8 KB  |  1,474 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: httrack.c subroutines:                                 */
  34. /*       savename routine (compute output filename)             */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. /* Internal engine bytecode */
  39. #define HTS_INTERNAL_BYTECODE
  40.  
  41. #include "htsname.h"
  42.  
  43. /* specific definitions */
  44. #include "htsbase.h"
  45. #include "htstools.h"
  46. #include "htsmd5.h"
  47. #include <ctype.h>
  48. /* END specific definitions */
  49.  
  50. #undef test_flush
  51. #define test_flush if (opt->flush) { fflush(opt->log); fflush(opt->errlog); }
  52.  
  53. #define ADD_STANDARD_PATH \
  54.     {  /* ajout nom */\
  55.       char BIGSTK buff[HTS_URLMAXSIZE*2];\
  56.       buff[0]='\0';\
  57.       strncatbuff(buff,start_pos,(int) (nom_pos - start_pos));\
  58.       url_savename_addstr(save,buff);\
  59.     }
  60.  
  61. #define ADD_STANDARD_NAME(shortname) \
  62.     {  /* ajout nom */\
  63.       char BIGSTK buff[HTS_URLMAXSIZE*2];\
  64.       standard_name(buff,dot_pos,nom_pos,fil_complete,(shortname));\
  65.       url_savename_addstr(save,buff);\
  66.     }
  67.  
  68.  
  69. /* Avoid stupid DOS system folders/file such as 'nul' */
  70. /* Based on linux/fs/umsdos/mangle.c */
  71. static const char *hts_tbdev[] =
  72. {
  73.     "/prn", "/con", "/aux", "/nul",
  74.     "/lpt1", "/lpt2", "/lpt3", "/lpt4",
  75.     "/com1", "/com2", "/com3", "/com4",
  76.     "/clock$",
  77.     "/emmxxxx0", "/xmsxxxx0", "/setverxx",
  78.     ""
  79. };
  80.  
  81.  
  82. #define URLSAVENAME_WAIT_FOR_AVAILABLE_SOCKET() do { \
  83.   int prev = _hts_in_html_parsing; \
  84.   while(back_pluggable_sockets_strict(sback, opt) <= 0) { \
  85.     _hts_in_html_parsing = 6; \
  86.     /* Wait .. */ \
  87.     back_wait(sback,opt,cache,0); \
  88.     /* Transfer rate */ \
  89.     engine_stats(); \
  90.     /* Refresh various stats */ \
  91.     HTS_STAT.stat_nsocket=back_nsoc(sback); \
  92.     HTS_STAT.stat_errors=fspc(NULL,"error"); \
  93.     HTS_STAT.stat_warnings=fspc(NULL,"warning"); \
  94.     HTS_STAT.stat_infos=fspc(NULL,"info"); \
  95.     HTS_STAT.nbk=backlinks_done(sback,liens,lien_tot,ptr); \
  96.     HTS_STAT.nb=back_transfered(HTS_STAT.stat_bytes,sback); \
  97.     /* Check */ \
  98.     if (!hts_htmlcheck_loop(sback->lnk, sback->count,-1,ptr,lien_tot,(int) (time_local()-HTS_STAT.stat_timestart),&HTS_STAT)) { \
  99.       return -1; \
  100.     } \
  101.   } \
  102.   _hts_in_html_parsing = prev; \
  103. } while(0)
  104.  
  105.  
  106. // forme le nom du fichier α sauver (save) α partir de fil et adr
  107. // systΦme intelligent, qui renomme en cas de besoin (exemple: deux INDEX.HTML et index.html)
  108. int url_savename(char* adr_complete, char* fil_complete, char* save, 
  109.                                  char* former_adr, char* former_fil, 
  110.                                  char* referer_adr, char* referer_fil, 
  111.                                  httrackp* opt, 
  112.                                  lien_url** liens, int lien_tot, 
  113.                                  struct_back* sback, cache_back* cache, hash_struct* hash, 
  114.                                  int ptr, int numero_passe, const lien_back* headers) {
  115.     const char* mime_type = headers ? headers->r.contenttype : NULL;
  116.   lien_back* const back = sback->lnk;
  117.   const int back_max = sback->count;
  118.   /* */
  119.   char BIGSTK newfil[HTS_URLMAXSIZE*2];   /* ="" */
  120.   /*char BIGSTK normadr_[HTS_URLMAXSIZE*2];*/
  121.   char BIGSTK normadr_[HTS_URLMAXSIZE*2], normfil_[HTS_URLMAXSIZE*2];
  122.     enum { PROTOCOL_HTTP, PROTOCOL_HTTPS, PROTOCOL_FTP, PROTOCOL_FILE, PROTOCOL_MMS, PROTOCOL_UNKNOWN };
  123.   static const char* protocol_str[] = {"http", "https", "ftp", "file", "mms", "unknown"};
  124.   int protocol = PROTOCOL_HTTP;
  125.   char* normadr;
  126.   char* normfil;
  127.   char* fil;
  128.   char* adr;
  129.   char* print_adr;
  130.   char *start_pos=NULL,*nom_pos=NULL,*dot_pos=NULL;  // Position nom et point
  131.   // pour changement d'extension ou de nom (content-disposition)
  132.   int ext_chg=0;
  133.   int is_html=0;
  134.   char ext[256];
  135.   int max_char=0;
  136.   //CLEAR
  137.   newfil[0]=ext[0]='\0';
  138.  
  139.   /* 8-3 ? */
  140.   switch(opt->savename_83) {
  141.   case 1:     // 8-3
  142.     max_char=8;
  143.     break;
  144.   case 2:     // Level 2 File names may be up to 31 characters.
  145.     max_char=31;
  146.     break;
  147.   default:
  148.     max_char=8;
  149.     break;
  150.   }
  151.  
  152.   // effacer save
  153.   save[0]='\0';
  154.   // fil
  155.   fil = fil_complete;
  156.   // copy of fil, used for lookups (see urlhack)
  157.   normfil = fil;
  158.   // et adr (sauter user/pass)
  159.   // on prend le parti de mettre les fichiers avec login/pass au mΩme endroit que si ils
  160.   // Θtaient capturΘs sans ces paramΦtres
  161.   // c'est pour cette raison qu'on ignore totalement adr_complete (mΩme pour la recherche en table de hachage)
  162.   adr = jump_identification(adr_complete);
  163.   // copy of adr, used for lookups (see urlhack)
  164.   normadr = adr;
  165.  
  166.   // normalize the URL:
  167.   // www.foo.com -> foo.com
  168.   // www-42.foo.com -> foo.com
  169.   // foo.com/bar//foobar -> foo.com/bar/foobar
  170.   if (opt->urlhack) {
  171.     // copy of adr (without protocol), used for lookups (see urlhack)
  172.     normadr=adr_normalized(adr, normadr_);
  173.     normfil=fil_normalized(fil,normfil_);
  174.   } else {
  175.     if (link_has_authority(adr_complete)) {     // https or other protocols : in "http/" subfolder
  176.       char* pos = strchr(adr_complete, ':');
  177.       if (pos != NULL) {
  178.         normadr_[0] = '\0';
  179.         strncatbuff(normadr_, adr_complete, (int)(pos - adr_complete));
  180.         strcatbuff(normadr_, "://");
  181.         strcatbuff(normadr_, normadr);
  182.         normadr=normadr_;
  183.       }
  184.     }
  185.   }
  186.  
  187.   // α afficher sans ftp://
  188.   print_adr=jump_protocol(adr);
  189.   if (strfield(adr_complete, "https:")) {
  190.       protocol = PROTOCOL_HTTPS;
  191.   } else if (strfield(adr_complete, "ftp:")) {
  192.       protocol = PROTOCOL_FTP;
  193.   } else if (strfield(adr_complete, "file:")) {
  194.       protocol = PROTOCOL_FILE;
  195.   } else if (strfield(adr_complete, "mms:")) {
  196.       protocol = PROTOCOL_MMS;
  197.   } else {
  198.       protocol = PROTOCOL_HTTP;
  199.   }
  200.  
  201.   // court-circuit pour lien primaire
  202.   if (strnotempty(adr)==0) {
  203.     if (strcmp(fil,"primary")==0) {
  204.       strcatbuff(save,"primary.html");
  205.       return 0;
  206.     }
  207.   }
  208.  
  209.  
  210.   // vΘrifier que le nom n'a pas dΘja ΘtΘ calculΘ (si oui le renvoyer tel que)
  211.   // vΘrifier que le nom n'est pas dΘja pris...
  212.   // NOTE: si on cherche /toto/ et que /toto est trouvΘ on le prend (et rΘciproquqment) ** // **
  213.   if (liens!=NULL) { 
  214.     int i;
  215.  
  216.     i=hash_read(hash,normadr,normfil,1,opt->urlhack);      // recherche table 1 (adr+fil)
  217.     if (i>=0) {    // ok, trouvΘ
  218.       strcpybuff(save,liens[i]->sav);
  219.       return 0;
  220.     }
  221.     i=hash_read(hash,normadr,normfil,2,opt->urlhack);      // recherche table 2 (former_adr+former_fil)
  222.     if (i>=0) {    // ok, trouvΘ
  223.       // copier location moved!
  224.       strcpybuff(adr_complete,liens[i]->adr);
  225.       strcpybuff(fil_complete,liens[i]->fil);
  226.       // et save
  227.       strcpybuff(save,liens[i]->sav);  // copier (formΘ α partir du nouveau lien!)
  228.       return 0;
  229.     }
  230.  
  231.     // chercher sans / ou avec / dans former
  232.     {
  233.       char BIGSTK fil_complete_patche[HTS_URLMAXSIZE*2];
  234.       strcpybuff(fil_complete_patche,normfil);
  235.       // Version avec ou sans /
  236.       if (fil_complete_patche[strlen(fil_complete_patche)-1]=='/')
  237.         fil_complete_patche[strlen(fil_complete_patche)-1]='\0';
  238.       else
  239.         strcatbuff(fil_complete_patche,"/");
  240.       i=hash_read(hash,normadr,fil_complete_patche,2,opt->urlhack);      // recherche table 2 (former_adr+former_fil)
  241.       if (i>=0) {
  242.         // Θcraser fil et adr (pas former_fil?????)
  243.         strcpybuff(adr_complete,liens[i]->adr);
  244.         strcpybuff(fil_complete,liens[i]->fil);
  245.         // Θcrire save
  246.         strcpybuff(save,liens[i]->sav);
  247.         return 0;
  248.       }
  249.     }
  250.   }
  251.  
  252.   // vΘrifier la non prΘsence de paramΦtres dans le nom de fichier
  253.   // si il y en a, les supprimer (ex: truc.cgi?subj=aspirateur)
  254.   // nΘanmoins, gardΘ pour vΘrifier la non duplication (voir aprΦs)
  255.   {
  256.     char* a;
  257.     a=strchr(fil,'?');
  258.     if (a!=NULL) {
  259.       strncatbuff(newfil,fil,(int) (a - fil));
  260.     } else {
  261.       strcpybuff(newfil,fil);
  262.     }
  263.     fil=newfil;
  264.   }
  265.   // Decode remaining %
  266.   strcpybuff(fil,unescape_http(fil));
  267.     // , BUT do not decode high chars
  268.   //strcpybuff(fil,unescape_http_unharm(fil, 1));
  269.     // YES (not server side, but fs/client side)
  270.  
  271. #if HTS_USEMMS
  272.     /* .asx hack */
  273.     if (headers != NULL && headers->r.cdispo[0] != 0 
  274.         && strfield(headers->r.contenttype, "video/")
  275.         && strfield2(get_ext(headers->r.cdispo), "asx") == 0) 
  276.     {
  277.         ext_chg = 1;
  278.         strcpybuff(ext, "asx");
  279.     }
  280.     else if (headers != NULL && headers->r.contenttype[0] != 0 
  281.         && strfield2(headers->r.contenttype, "video/x-ms-asf"))
  282.     {
  283.         char *exts = get_ext(headers->url_fil);
  284.         if (strfield2(exts, "wmv") == 0)
  285.         {
  286.             ext_chg = 1;
  287.             strcpybuff(ext, "wmv");
  288.         }
  289.         else if (strfield2(exts, "asf") == 0)
  290.         {
  291.             ext_chg = 1;
  292.             strcpybuff(ext, "asf");
  293.         }
  294.         else if (strfield2(exts, "avi") == 0)
  295.         {
  296.             ext_chg = 1;
  297.             strcpybuff(ext, "avi");
  298.         }
  299.         else if (strfield2(exts, "asx") == 0)
  300.         {
  301.             ext_chg = 1;
  302.             strcpybuff(ext, "asx");
  303.         }
  304.     }
  305. #endif
  306.  
  307.   /* replace shtml to html.. */
  308.   if (opt->savename_delayed == 2)
  309.     is_html = -1;            /* ALWAYS delay type */
  310.   else
  311.     is_html = ishtml(fil);
  312.   switch ( is_html ) {       /* .html,.shtml,.. */
  313.   case 1:
  314.     if ( 
  315.       (strfield2(get_ext(fil),"html") == 0)
  316.       && (strfield2(get_ext(fil),"htm") == 0)
  317.       )
  318.     {
  319.       strcpybuff(ext,"html");
  320.       ext_chg=1;
  321.     }
  322.     break;
  323.   case 0:
  324.     if (!strnotempty(ext)) {
  325.       if (is_userknowntype(fil)) {      // mime known by user
  326.         char BIGSTK mime[1024];
  327.         mime[0]=ext[0]='\0';
  328.         get_userhttptype(0,mime,fil);
  329.         if (strnotempty(mime)) {
  330.           give_mimext(ext,mime);
  331.           if (strnotempty(ext)) {
  332.             ext_chg=1;
  333.           }
  334.         }
  335.       }
  336.     }
  337.     break;
  338.   }
  339.  
  340.   // si option check_type activΘe
  341.   if (is_html < 0 && opt->check_type && !ext_chg) {
  342.     int ishtest=0;
  343.     if ( (!strfield(adr_complete,"file://")) 
  344.       && (!strfield(adr_complete,"ftp://")) 
  345. #if HTS_USEMMS
  346.       && (!strfield(adr_complete,"mms://")) 
  347. #endif
  348.       ) {
  349.       // tester type avec requΦte HEAD si on ne connait pas le type du fichier
  350.       if (!(   (opt->check_type==1) && (fil[strlen(fil)-1]=='/')   ))    // slash doit Ωtre html?
  351.       if (opt->savename_delayed == 2 || (ishtest=ishtml(fil)) < 0) { // on ne sait pas si c'est un html ou un fichier..
  352.         // lire dans le cache
  353.         htsblk r = cache_read(opt,cache,adr,fil,NULL,NULL);              // test uniquement
  354.         if (r.statuscode != -1) {  // pas d'erreur de lecture cache
  355.           char s[16]; s[0]='\0';
  356.           if ( (opt->debug>1) && (opt->log!=NULL) ) {
  357.             fspc(opt->log,"debug"); fprintf(opt->log,"Testing link type (from cache) %s%s"LF,adr_complete,fil_complete);
  358.             test_flush;
  359.           }
  360.           if (strnotempty(r.cdispo)) {        /* filename given */
  361.             ext_chg=2;      /* change filename */
  362.             strcpybuff(ext,r.cdispo);
  363.           }
  364.           else if (!may_unknown(r.contenttype) || ishtest == -2) {  // on peut patcher α priori?
  365.             give_mimext(s,r.contenttype);  // obtenir extension
  366.             if (strnotempty(s)>0) {        // on a reconnu l'extension
  367.               ext_chg=1;
  368.               strcpybuff(ext,s);
  369.             }
  370.           }
  371.           //
  372.         } else if (opt->savename_delayed != 2 && is_userknowntype(fil)) {   /* PATCH BY BRIAN SCHR╓DER. 
  373.                                                                             Lookup mimetype not only by extension, 
  374.                                                                             but also by filename */
  375.           /* Note: "foo.cgi => text/html" means that foo.cgi shall have the text/html MIME file type,
  376.           that is, ".html" */
  377.           char BIGSTK mime[1024];
  378.           mime[0]=ext[0]='\0';
  379.           get_userhttptype(0, mime, fil);
  380.           if (strnotempty(mime)) {
  381.             give_mimext(ext, mime);
  382.             if (strnotempty(ext)) {
  383.               ext_chg=1;
  384.             }
  385.           }
  386.         }
  387.         // note: if savename_delayed is enabled, the naming will be temporary (and slightly invalid!)
  388.         else if (opt->savename_delayed != 0) {
  389.           if (mime_type != NULL)  {
  390.             ext[0] = '\0';
  391.             if (*mime_type) {
  392.               give_mimext(ext, mime_type);
  393.             }
  394.             if (strnotempty(ext)) {
  395.               ext_chg = 1;
  396.             }
  397.           } else {
  398.             /* Avoid collisions (no collisionning detection) */
  399.             sprintf(ext, "%x.%s", opt->state.delayedId++, DELAYED_EXT);
  400.             ext_chg = 1;
  401.           }
  402.         }
  403.         // test imposible dans le cache, faire une requΩte
  404.         else {
  405.           //
  406. #if HTS_ANALYSTE
  407.           int hihp=_hts_in_html_parsing;
  408. #endif
  409.           int has_been_moved=0;
  410.           char BIGSTK curr_adr[HTS_URLMAXSIZE*2],curr_fil[HTS_URLMAXSIZE*2];
  411.           
  412.           /* Ensure we don't use too many sockets by using a "testing" one
  413.              If we have only 1 simultaneous connection authorized, wait for pending download
  414.              Wait for an available slot 
  415.           */
  416.           URLSAVENAME_WAIT_FOR_AVAILABLE_SOCKET();
  417.  
  418.           /* Rock'in */
  419.           curr_adr[0]=curr_fil[0]='\0';
  420. #if HTS_ANALYSTE
  421.           _hts_in_html_parsing=2;  // test
  422. #endif
  423.           if ( (opt->debug>1) && (opt->log!=NULL) ) {
  424.             fspc(opt->log,"debug"); fprintf(opt->log,"Testing link type %s%s"LF,adr_complete,fil_complete);
  425.             test_flush;
  426.           }
  427.           strcpybuff(curr_adr,adr_complete);
  428.           strcpybuff(curr_fil,fil_complete);
  429.           // ajouter dans le backing le fichier en mode test
  430.           // savename: rien car en mode test
  431.           if (back_add(sback,opt,cache,curr_adr,curr_fil,BACK_ADD_TEST,referer_adr,referer_fil,1,NULL)!=-1) {
  432.             int b;
  433.             b=back_index(sback,curr_adr,curr_fil,BACK_ADD_TEST);         
  434.             if (b>=0) {
  435.               int stop_looping=0;
  436.               int petits_tours=0;
  437.               int get_test_request=0;       // en cas de bouclage sur soi mΩme avec HEAD, tester avec GET.. parfois c'est la cause des problΦmes
  438.               do {
  439.                 // temps α attendre, et remplir autant que l'on peut le cache (backing)
  440.                 if (back[b].status>0) {
  441.                   back_wait(sback,opt,cache,0);        
  442.                 }
  443.                 if (ptr>=0) {
  444.                   back_fillmax(sback,opt,cache,liens,ptr,numero_passe,lien_tot);
  445.                 }
  446.   
  447.                 // on est obligΘ d'appeler le shell pour le refresh..
  448. #if HTS_ANALYSTE
  449.                 {
  450.                   
  451.                   // Transfer rate
  452.                   engine_stats();
  453.                   
  454.                   // Refresh various stats
  455.                   HTS_STAT.stat_nsocket=back_nsoc(sback);
  456.                   HTS_STAT.stat_errors=fspc(NULL,"error");
  457.                   HTS_STAT.stat_warnings=fspc(NULL,"warning");
  458.                   HTS_STAT.stat_infos=fspc(NULL,"info");
  459.                   HTS_STAT.nbk=backlinks_done(sback,liens,lien_tot,ptr);
  460.                   HTS_STAT.nb=back_transfered(HTS_STAT.stat_bytes,sback);
  461.  
  462.                   if (!hts_htmlcheck_loop(sback->lnk, sback->count,b,ptr,lien_tot,(int) (time_local()-HTS_STAT.stat_timestart),&HTS_STAT)) {
  463.                     return -1;
  464.                   } else if (_hts_cancel || !back_checkmirror(opt)) {    // cancel 2 ou 1 (cancel parsing)
  465.                     back_delete(opt,cache,sback,b);       // cancel test
  466.                     stop_looping = 1;
  467.                   }
  468.                 }
  469. #endif
  470.                 
  471.                 
  472.                 // traitement des 304,303..
  473.                 if (back[b].status<=0) {
  474.                   if (HTTP_IS_REDIRECT(back[b].r.statuscode)) {    // agh moved.. un tit tour de plus
  475.                     if ((petits_tours<5) && (former_adr) && (former_fil)) { // on va pas tourner en rond non plus!
  476.                       if ((int) strnotempty(back[b].r.location)) {    // location existe!
  477.                         char BIGSTK mov_url[HTS_URLMAXSIZE*2],mov_adr[HTS_URLMAXSIZE*2],mov_fil[HTS_URLMAXSIZE*2];
  478.                         mov_url[0]=mov_adr[0]=mov_fil[0]='\0';
  479.                         //
  480.                         strcpybuff(mov_url,back[b].r.location);    // copier URL
  481.                         if (ident_url_relatif(mov_url,curr_adr,curr_fil,mov_adr,mov_fil)>=0) {                        
  482.                           // si non bouclage sur soi mΩme, ou si test avec GET non testΘ
  483.                           if ((strcmp(mov_adr,curr_adr)) || (strcmp(mov_fil,curr_fil)) || (get_test_request==0)) {
  484.                             // bouclage?
  485.                             if ((!strcmp(mov_adr,curr_adr)) && (!strcmp(mov_fil,curr_fil)))
  486.                               get_test_request=1;     // faire requΦte avec GET
  487.  
  488.                             // recopier former_adr/fil?
  489.                             if ((former_adr) && (former_fil)) {
  490.                               if (strnotempty(former_adr)==0) {    // Pas dΘja notΘ
  491.                                 strcpybuff(former_adr,curr_adr);
  492.                                 strcpybuff(former_fil,curr_fil);
  493.                               }
  494.                             }
  495.  
  496.                             // check explicit forbidden - don't follow 3xx in this case
  497.                             {
  498.                               int set_prio_to=0;
  499.                               robots_wizard* robots = (robots_wizard*) opt->robotsptr;
  500.                               if (hts_acceptlink(opt,ptr,lien_tot,liens,
  501.                                 mov_adr,mov_fil,
  502.                                 NULL, NULL,
  503.                                 &set_prio_to,
  504.                                 NULL) == 1) 
  505.                               {  /* forbidden */
  506.                                 has_been_moved = 1;
  507.                                 back_maydelete(opt,cache,sback,b);    // ok
  508.                                 strcpybuff(curr_adr,mov_adr);
  509.                                 strcpybuff(curr_fil,mov_fil);
  510.                                 mov_url[0]='\0';
  511.                                 stop_looping = 1;
  512.                               }
  513.                             }
  514.                             
  515.                             // ftp: stop!
  516.                             if (strfield(mov_url,"ftp://")
  517. #if HTS_USEMMS
  518.                                                             || strfield(mov_url,"mms://")
  519. #endif
  520.                                                             ) 
  521.                                                         {    // ftp, ok on arrΩte
  522.                               has_been_moved = 1;
  523.                               back_maydelete(opt,cache,sback,b);    // ok
  524.                               strcpybuff(curr_adr,mov_adr);
  525.                               strcpybuff(curr_fil,mov_fil);
  526.                               stop_looping = 1;
  527.                             } else if (*mov_url) {
  528.                               char* methode;
  529.                               if (!get_test_request)
  530.                                 methode=BACK_ADD_TEST;      // tester avec HEAD
  531.                               else {
  532.                                 methode=BACK_ADD_TEST2;     // tester avec GET
  533.                                 if ( opt->errlog!=NULL ) {
  534.                                   fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Loop with HEAD request (during prefetch) at %s%s"LF,curr_adr,curr_fil);
  535.                                   test_flush;
  536.                                 }                    
  537.                               }
  538.                               // Ajouter
  539.                               URLSAVENAME_WAIT_FOR_AVAILABLE_SOCKET();
  540.                               if (back_add(sback,opt,cache,mov_adr,mov_fil,methode,referer_adr,referer_fil,1,NULL)!=-1) {    // OK
  541.                                 if ( (opt->debug>1) && (opt->errlog!=NULL) ) {
  542.                                   fspc(opt->errlog,"warning"); fprintf(opt->errlog,"(during prefetch) %s (%d) to link %s at %s%s"LF,back[b].r.msg,back[b].r.statuscode,back[b].r.location,curr_adr,curr_fil);
  543.                                   test_flush;
  544.                                 }
  545.                                 
  546.                                 // libΘrer emplacement backing actuel et attendre le prochain
  547.                                 back_maydelete(opt,cache,sback,b);
  548.                                 strcpybuff(curr_adr,mov_adr);
  549.                                 strcpybuff(curr_fil,mov_fil);
  550.                                 b=back_index(sback,curr_adr,curr_fil,methode);         
  551.                                 if (!get_test_request)
  552.                                   has_been_moved = 1;       // sinon ne pas forcer has_been_moved car non dΘplacΘ
  553.                                 petits_tours++;
  554.                                 //
  555.                               } else {// sinon on fait rien et on s'en va.. (ftp etc)
  556.                                 if ( (opt->debug>1)  && (opt->errlog)) {
  557.                                   fspc(opt->errlog,"debug"); fprintf(opt->errlog,"Warning: Savename redirect backing error at %s%s"LF,mov_adr,mov_fil);
  558.                                   test_flush;
  559.                                 } 
  560.                               }
  561.                             }
  562.                           } else {
  563.                             if ( opt->errlog!=NULL ) {
  564.                               fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Unable to test %s%s (loop to same filename)"LF,adr_complete,fil_complete);
  565.                               test_flush;
  566.                             }
  567.                           }
  568.                           
  569.                         }
  570.                       }
  571.                     } else{  // arrΩter les frais
  572.                       if ( opt->errlog!=NULL ) {
  573.                         fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Unable to test %s%s (loop)"LF,adr_complete,fil_complete);
  574.                         test_flush;
  575.                       }
  576.                     }
  577.                   }  // ok, leaving
  578.                 }
  579.               } while(!stop_looping && back[b].status > 0 && back[b].status < 1000);
  580.               
  581.               // Si non dΘplacΘ, forcer type?
  582.               if (!has_been_moved) {
  583.                 if (back[b].r.statuscode!=-10) {    // erreur
  584.                   if (strnotempty(back[b].r.contenttype)==0)
  585.                     strcpybuff(back[b].r.contenttype,"text/html");    // message d'erreur en html
  586.                   // Finalement on, renvoie un erreur, pour ne toucher α rien dans le code
  587.                   // libΘrer emplacement backing
  588.                   /*if (opt->errlog!=NULL) {
  589.                     fspc(opt->errlog,0); fprintf(opt->errlog,"Error: (during prefetch) %s (%d) to link %s at %s%s"LF,back[b].r.msg,back[b].r.statuscode,back[b].r.location,curr_adr,curr_fil);
  590.                     test_flush;
  591.                   }                    
  592.                   back_delete(opt,cache,sback,b);
  593.                   return -1;        // ERREUR (404 par exemple)
  594.                   */
  595.                 } 
  596.                 
  597.                 {            // pas d'erreur, changer type?
  598.                   char s[16];
  599.                   s[0]='\0';
  600.                   if (strnotempty(back[b].r.cdispo)) {        /* filename given */
  601.                     ext_chg=2;      /* change filename */
  602.                     strcpybuff(ext,back[b].r.cdispo);
  603.                   }
  604.                   else if (!may_unknown(back[b].r.contenttype) || ishtest == -2 ) {  // on peut patcher α priori? (pas interdit ou pas de type)
  605.                     give_mimext(s,back[b].r.contenttype);  // obtenir extension
  606.                     if (strnotempty(s)>0) {    // on a reconnu l'extension
  607.                       ext_chg=1;
  608.                       strcpybuff(ext,s);
  609.                     }
  610.                   }
  611.                 }
  612.               }
  613.               // FIN Si non dΘplacΘ, forcer type?
  614.  
  615.               // libΘrer emplacement backing
  616.               back_maydelete(opt,cache,sback,b);
  617.               
  618.               // --- --- ---
  619.               // oops, a ΘtΘ dΘplacΘ.. on recalcule en rΘcursif (osons!)
  620.               if (has_been_moved) {
  621.                 // copier adr, fil (optionnel, mais sinon marche pas pour le rip)
  622.                 strcpybuff(adr_complete,curr_adr);
  623.                 strcpybuff(fil_complete,curr_fil);
  624.                 // copier adr, fil
  625.                 
  626.                 return url_savename(curr_adr,curr_fil,save,NULL,NULL,referer_adr,referer_fil,opt,liens,lien_tot,sback,cache,hash,ptr,numero_passe,NULL);
  627.               }
  628.               // --- --- ---
  629.               
  630.             }
  631.             
  632.           } else {
  633.             printf("PANIC! : Savename Crash adding error, unexpected error found.. [%d]\n",__LINE__);
  634. #if BDEBUG==1
  635.             printf("error while savename crash adding\n");
  636. #endif
  637.             if (opt->errlog) {
  638.               fspc(opt->errlog,"error"); fprintf(opt->errlog,"Unexpected savename backing error at %s%s"LF,adr,fil_complete);
  639.               test_flush;
  640.             } 
  641.             
  642.           }
  643.           // restaurer
  644. #if HTS_ANALYSTE
  645.           _hts_in_html_parsing=hihp;
  646. #endif
  647.         }  // cachΘ?
  648.       }
  649.     }
  650.   }
  651.  
  652.  
  653.  
  654.   // - - - DEBUT NOMMAGE - - -
  655.  
  656.   // Donner nom par dΘfaut?
  657.   if (fil[strlen(fil)-1]=='/')  {
  658.     if (!strfield(adr_complete,"ftp://")
  659. #if HTS_USEMMS
  660.             && !strfield(adr_complete,"mms://")
  661. #endif
  662.             )
  663.         {
  664.       strcatbuff(fil,DEFAULT_HTML);     // nommer page par dΘfaut!!
  665. #if HTS_USEMMS
  666.         } else if (strfield(adr_complete,"mms://")) {
  667.             strcatbuff(fil,DEFAULT_MMS);
  668. #endif
  669.         } else {
  670.       if (!opt->proxy.active)
  671.         strcatbuff(fil,DEFAULT_FTP);     // nommer page par dΘfaut (texte)
  672.       else
  673.         strcatbuff(fil,DEFAULT_HTML);     // nommer page par dΘfaut (α priori ici html depuis un proxy http)
  674.     }
  675.   }
  676.   // Changer extension?
  677.   // par exemple, php3 sera sauvΘ en html, cgi en html ou gif, xbm etc.. selon les cas
  678.   if (ext_chg) {  // changer ext
  679.     char* a=fil+strlen(fil)-1;
  680.     if ( (opt->debug>1) && (opt->log!=NULL) ) {
  681.       fspc(opt->log,"debug");
  682.       if (ext_chg==1)
  683.         fprintf(opt->log,"Changing link extension %s%s to .%s"LF,adr_complete,fil_complete,ext);
  684.       else
  685.         fprintf(opt->log,"Changing link name %s%s to %s"LF,adr_complete,fil_complete,ext);
  686.       test_flush;
  687.     }
  688.     if (ext_chg==1) {
  689.       while((a > fil) && (*a!='.') && (*a!='/')) a--;
  690.       if (*a=='.') *a='\0';  // couper
  691.       strcatbuff(fil,".");      // recopier point
  692.     } else {
  693.       while(( a > fil) && (*a!='/')) a--;
  694.       if (*a=='/') a++;
  695.       *a='\0';
  696.    }
  697.     strcatbuff(fil,ext);    // copier ext/nom
  698.   }
  699.  
  700.   // Rechercher premier / et dernier .
  701.   {  
  702.     char* a=fil+strlen(fil)-1;
  703.  
  704.     // passer structures
  705.     start_pos=fil;
  706.     while(( a > fil) && (*a != '/') && (*a != '\\')) {
  707.       if (*a == '.')    // point? noter position
  708.         if (!dot_pos)
  709.           dot_pos=a;
  710.         a--;
  711.     }
  712.     if ((*a=='/') || (*a=='\\')) a++;
  713.     nom_pos = a;
  714.   }
  715.  
  716.   
  717.   // un nom de fichier est gΘnΘrΘ
  718.   // s'il existe dΘja, alors on le mofifie lΘgΦrement
  719.  
  720.   // ajouter nom du site Θventuellement en premier
  721.   if (opt->savename_type == -1) {    // utiliser savename_userdef! (%h%p/%n%q.%t)
  722.     char* a = opt->savename_userdef;
  723.     char* b = save;
  724.     /*char *nom_pos=NULL,*dot_pos=NULL;  // Position nom et point */
  725.     char tok;
  726.  
  727.     /*
  728.     {  // Rechercher premier /
  729.       char* a=fil+strlen(fil)-1;
  730.       // passer structures
  731.       while(((int) a>(int) fil) && (*a != '/') && (*a != '\\')) {
  732.         if (*a == '.')    // point? noter position
  733.         if (!dot_pos)
  734.           dot_pos=a;
  735.         a--;
  736.       }
  737.       if ((*a=='/') || (*a=='\\')) a++;
  738.       nom_pos = a;
  739.     }
  740.     */
  741.  
  742.     // Construire nom
  743.     while ((*a) && (((int) (b - save)) < HTS_URLMAXSIZE ) ) {    // parser, et pas trop long..
  744.       if (*a == '%') {
  745.         int short_ver=0;
  746.         a++;
  747.         if (*a == 's') {
  748.           short_ver=1;
  749.           a++;
  750.         }
  751.         *b='\0';
  752.         switch(tok=*a++) {
  753.           case '[':            // %[param:prefix_if_not_empty:suffix_if_not_empty:empty_replacement:notfound_replacement]
  754.             if (strchr(a,']')) {
  755.               int pos=0;
  756.               char name[5][256];
  757.               char* c=name[0];
  758.               for(pos = 0 ; pos < 5 ; pos++) {
  759.                 name[pos][0]='\0';
  760.               }
  761.               pos=0;
  762.               while(*a!=']') {
  763.                 if (pos < 5) {
  764.                   if (*a == ':') {  // next token
  765.                     c=name[++pos];
  766.                     a++;
  767.                   } else {
  768.                     *c++=*a++;
  769.                     *c='\0';
  770.                   }
  771.                 }
  772.               }
  773.               a++;
  774.               strcatbuff(name[0],"=");           /* param=.. */
  775.               c=strchr(fil_complete,'?');
  776.               /* parameters exists */
  777.               if (c) {
  778.                 char* cp;
  779.                 while((cp = strstr(c+1, name[0])) && *(cp-1) != '?' && *(cp-1) != '&') {     /* finds [?&]param= */
  780.                   c = cp;
  781.                 }
  782.                 if (cp) {
  783.                   c = cp + strlen(name[0]);    /* jumps "param=" */
  784.                   strcpybuff(b, name[1]);    /* prefix */
  785.                   b += strlen(b);
  786.                   if (*c != '\0' && *c != '&') {
  787.                     char* d = name[0];
  788.                     /* */
  789.                     while(*c != '\0' && *c != '&') {
  790.                       *d++ = *c++;
  791.                     }
  792.                     *d = '\0';
  793.                     d = unescape_http(name[0]);
  794.                     if (d && *d) {
  795.                       strcpybuff(b, d);         /* value */
  796.                       b += strlen(b);
  797.                     } else {
  798.                       strcpybuff(b, name[3]);    /* empty replacement if any */
  799.                       b += strlen(b);
  800.                     }
  801.                   } else {
  802.                     strcpybuff(b, name[3]);    /* empty replacement if any */
  803.                     b += strlen(b);
  804.                   }
  805.                   strcpybuff(b, name[2]);    /* suffix */
  806.                   b += strlen(b);
  807.                 } else {
  808.                   strcpybuff(b, name[4]);    /* not found replacement if any */
  809.                   b += strlen(b);
  810.                 }
  811.                             } else {
  812.                                 strcpybuff(b, name[4]);    /* not found replacement if any */
  813.                                 b += strlen(b);
  814.                             }
  815.             }
  816.           break;
  817.           case '%': *b++='%'; break;
  818.           case 'n':    // nom sans ext
  819.             *b='\0';
  820.             if (dot_pos) {
  821.               if (!short_ver)    // Noms longs
  822.                 strncatbuff(b,nom_pos,(int) (dot_pos - nom_pos));
  823.               else
  824.                 strncatbuff(b,nom_pos,min((int) (dot_pos - nom_pos),8));
  825.             } else {
  826.               if (!short_ver)    // Noms longs
  827.                 strcpybuff(b,nom_pos);
  828.               else
  829.                 strncatbuff(b,nom_pos,8);
  830.             }
  831.             b+=strlen(b);   // pointer α la fin
  832.             break;
  833.           case 'N':    // nom avec ext
  834.             // RECOPIE NOM + EXT
  835.             *b='\0';
  836.             if (dot_pos) {
  837.               if (!short_ver)    // Noms longs
  838.                 strncatbuff(b,nom_pos,(int) (dot_pos - nom_pos));
  839.               else
  840.                 strncatbuff(b,nom_pos,min((int) (dot_pos - nom_pos),8));
  841.             } else {
  842.               if (!short_ver)    // Noms longs
  843.                 strcpybuff(b,nom_pos);
  844.               else
  845.                 strncatbuff(b,nom_pos,8);
  846.             }
  847.             b+=strlen(b);   // pointer α la fin
  848.             // RECOPIE NOM + EXT
  849.             *b='\0';
  850.             if (dot_pos) {
  851.               if (!short_ver)    // Noms longs
  852.                 strcpybuff(b,dot_pos+1);
  853.               else
  854.                 strncatbuff(b,dot_pos+1,3);
  855.             } else {
  856.               if (!short_ver)    // Noms longs
  857.                 strcpybuff(b,DEFAULT_EXT + 1);    // pas de..
  858.               else
  859.                 strcpybuff(b,DEFAULT_EXT_SHORT + 1);    // pas de..
  860.             }
  861.             b+=strlen(b);   // pointer α la fin
  862.             //
  863.             break;
  864.           case 't':    // ext
  865.             *b='\0';
  866.             if (dot_pos) {
  867.               if (!short_ver)    // Noms longs
  868.                 strcpybuff(b,dot_pos+1);
  869.               else
  870.                 strncatbuff(b,dot_pos+1,3);
  871.             } else {
  872.               if (!short_ver)    // Noms longs
  873.                 strcpybuff(b,DEFAULT_EXT + 1);    // pas de..
  874.               else
  875.                 strcpybuff(b,DEFAULT_EXT_SHORT + 1);    // pas de..
  876.             }
  877.             b+=strlen(b);   // pointer α la fin
  878.             break;
  879.           case 'p':    // path sans dernier /
  880.             *b='\0';
  881.             if (nom_pos != fil + 1) { // pas: /index.html (chemin nul)
  882.               if (!short_ver) {   // Noms longs
  883.                 strncatbuff(b,fil,(int) (nom_pos - fil) - 1);
  884.               } else {
  885.                 char BIGSTK pth[HTS_URLMAXSIZE*2],n83[HTS_URLMAXSIZE*2];
  886.                 pth[0]=n83[0]='\0';
  887.                 //
  888.                 strncatbuff(pth,fil,(int) (nom_pos - fil) - 1);
  889.                 long_to_83(opt->savename_83,n83,pth);
  890.                 strcpybuff(b,n83);
  891.               }
  892.             }
  893.             b+=strlen(b);   // pointer α la fin
  894.             break;
  895.           case 'h':    // host
  896.             *b='\0';
  897.             if (strcmp(adr_complete,"file://")==0) {
  898.               if (!short_ver)    // Noms longs
  899.                 strcpybuff(b,"localhost");
  900.               else
  901.                 strcpybuff(b,"local");
  902.             } else {
  903.               if (!short_ver)    // Noms longs
  904.                 strcpybuff(b,print_adr);
  905.               else
  906.                 strncatbuff(b,print_adr,8);
  907.             }
  908.             b+=strlen(b);   // pointer α la fin
  909.             break;
  910.           case 'M':         /* host/address?query MD5 (128-bits) */
  911.             *b='\0';
  912.             {
  913.               char digest[32+2];
  914.               char BIGSTK buff[HTS_URLMAXSIZE*2];
  915.               digest[0]=buff[0]='\0';
  916.               strcpybuff(buff,adr);
  917.               strcatbuff(buff,fil_complete);
  918.               domd5mem(buff,strlen(buff),digest,1);
  919.               strcpybuff(b,digest);
  920.             }
  921.             b+=strlen(b);   // pointer α la fin
  922.             break;
  923.           case 'Q': case 'q':         /* query MD5 (128-bits/16-bits) 
  924.                                          GENERATED ONLY IF query string exists! */
  925.             *b='\0';
  926.             strncatbuff(b,url_md5(fil_complete),(tok == 'Q')?32:4);
  927.             b+=strlen(b);   // pointer α la fin
  928.             break;
  929.           case 'r': case 'R':        // protocol
  930.             *b='\0';
  931.             strcatbuff(b, protocol_str[protocol]);
  932.             b+=strlen(b);   // pointer α la fin
  933.           break;
  934.  
  935.       /* Patch by Juan Fco Rodriguez to get the full query string */
  936.       case 'k':
  937.         {
  938.           char *d = strchr(fil_complete,'?');
  939.           if( d != NULL )
  940.           {
  941.             strcatbuff(b, d);
  942.             b+=strlen(b);
  943.           }
  944.         }
  945.         break;
  946.  
  947.         }
  948.       } else
  949.         *b++=*a++;
  950.     }
  951.     *b++='\0';
  952.     //
  953.     // Types prΘdΘfinis
  954.     //
  955.  
  956.   } 
  957.   //
  958.   // Structure originale
  959.   else if (opt->savename_type%100==0) { 
  960.     /* recopier www.. */
  961.     if (opt->savename_type!=100) {  
  962.       if (((opt->savename_type/1000)%2)==0) {    // >1000 signifie "pas de www/"
  963.         if (strcmp(adr_complete,"file://")==0) {
  964.           //## if (*adr==lOCAL_CHAR) {
  965.           if (opt->savename_83 != 1)  // noms longs
  966.             strcatbuff(save,"localhost");
  967.           else
  968.             strcatbuff(save,"local");
  969.         } else {
  970.           // adresse url
  971.           if (!opt->savename_83) {  // noms longs (et pas de .)
  972.             strcatbuff(save,print_adr);
  973.           } else {  // noms 8-3
  974.             if (strlen(print_adr)>4) {
  975.               if (strfield(print_adr,"www."))
  976.                 strncatbuff(save,print_adr+4,max_char);
  977.               else
  978.                 strncatbuff(save,print_adr,8);
  979.             } else strncatbuff(save,print_adr,max_char);
  980.           }
  981.         }
  982.         if (*fil!='/') strcatbuff(save,"/");
  983.       }
  984.     }
  985.   
  986.     hts_lowcase(save);
  987.         
  988.     /*
  989.     // ne sert α rien car a dΘja ΘtΘ filtrΘ normalement
  990.     if ((*fil=='.') && (*(fil+1)=='/'))          // ./index.html ** //
  991.       url_savename_addstr(save,fil+2);
  992.     else                                               // index.html ou /index.html
  993.       url_savename_addstr(save,fil);
  994.     if (save[strlen(save)-1]=='/') 
  995.       strcatbuff(save,DEFAULT_HTML);     // nommer page par dΘfaut!!
  996. */
  997.  
  998.     /* add name */
  999.     ADD_STANDARD_PATH;
  1000.     ADD_STANDARD_NAME(0);
  1001.  
  1002.   }
  1003.   //
  1004.   // Structure html/image
  1005.   else {    
  1006.     // dossier "web" ou "www.xxx" ?
  1007.     if (((opt->savename_type/1000)%2)==0) {    // >1000 signifie "pas de www/"
  1008.       if ((opt->savename_type/100)%2) {
  1009.         if (strcmp(adr_complete,"file://")==0) {
  1010.         //## if (*adr==lOCAL_CHAR) {
  1011.           if (opt->savename_83 != 1)  // noms longs
  1012.             strcatbuff(save,"localhost/");
  1013.           else
  1014.             strcatbuff(save,"local/");
  1015.         } else {
  1016.           // adresse url
  1017.           if (!opt->savename_83) {  // noms longs
  1018.             strcatbuff(save,print_adr); strcatbuff(save,"/");
  1019.           } else {  // noms 8-3
  1020.             if (strlen(print_adr)>4) {
  1021.               if (strfield(print_adr,"www."))
  1022.                 strncatbuff(save,print_adr+4,max_char);
  1023.               else
  1024.                 strncatbuff(save,print_adr,max_char);
  1025.               strcatbuff(save,"/");
  1026.             } else { 
  1027.               strncatbuff(save,print_adr,max_char); strcatbuff(save,"/");
  1028.             }
  1029.           }
  1030.         }
  1031.       } else {
  1032.         strcatbuff(save,"web/");    // rΘpertoire gΘnΘral
  1033.       }
  1034.     } 
  1035.  
  1036.     // si un html α coup s√r
  1037.     if ( (ext_chg!=0) ? (ishtml_ext(ext) == 1) : (ishtml(fil)==1) ) {
  1038.       if (opt->savename_type%100==2) {  // html/
  1039.         strcatbuff(save, "html/");
  1040.       }
  1041.     } else {
  1042.       if ((opt->savename_type%100==1) || (opt->savename_type%100==2)) {  // html & images
  1043.         strcatbuff(save, "images/");
  1044.       }
  1045.     }
  1046.     
  1047.     switch (opt->savename_type%100) {
  1048.     case 4: case 5: {           // sΘparer par types
  1049.       char* a=fil+strlen(fil)-1;
  1050.       // passer structures
  1051.       while(( a > fil) && (*a != '/') && (*a != '\\')) a--;      
  1052.       if ((*a=='/') || (*a=='\\')) a++;
  1053.  
  1054.       // html?
  1055.       if ( (ext_chg!=0) ? (ishtml_ext(ext)==1) : (ishtml(fil)==1) ) {
  1056.         if (opt->savename_type%100==5)
  1057.           strcatbuff(save,"html/");
  1058.       } else {
  1059.         char* a=fil+strlen(fil)-1;
  1060.         while(( a> fil) && (*a != '/') && (*a != '.')) a--;      
  1061.         if (*a!='.')
  1062.           strcatbuff(save,"other");
  1063.         else
  1064.           strcatbuff(save,a+1);
  1065.         strcatbuff(save,"/");
  1066.       }
  1067.       /*strcatbuff(save,a);*/
  1068.       /* add name */
  1069.       ADD_STANDARD_NAME(0);
  1070.             }
  1071.       break;
  1072.     case 99: {                  // 'codΘ' .. c'est un gadget
  1073.       int i;
  1074.       int j;
  1075.       char* a;
  1076.       char C[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
  1077.       int L;
  1078.       // pseudo-CRC sur fil et adr pour initialiser gΘnΘrateur alΘatoire..
  1079.       unsigned int s=0;
  1080.       L=strlen(C);
  1081.       for(i=0;i<(int) strlen(fil_complete);i++) {
  1082.         s+=(unsigned int) fil_complete[i];
  1083.       }
  1084.       for(i=0;i<(int) strlen(adr_complete);i++) {
  1085.         s+=(unsigned int) adr_complete[i];
  1086.       }
  1087.       srand(s);
  1088.       
  1089.       j=strlen(save);
  1090.       for(i=0;i<8;i++) {
  1091.         char c=C[(rand()%L)];
  1092.         save[i+j]=c;
  1093.       }
  1094.       save[i+j]='\0';
  1095.       // ajouter extension
  1096.       a=fil+strlen(fil)-1;
  1097.       while(( a > fil) && (*a != '/') && (*a != '.')) a--;
  1098.       if (*a=='.') {
  1099.         strcatbuff(save,a);    // ajouter
  1100.       }
  1101.              } 
  1102.       break;
  1103.     default: {   // noms sans les noms des rΘpertoires
  1104.       // ne garder que le nom, pas la structure
  1105.       /*
  1106.       char* a=fil+strlen(fil)-1;
  1107.       while(((int) a>(int) fil) && (*a != '/') && (*a != '\\')) a--;      
  1108.       if ((*a=='/') || (*a=='\\')) a++;
  1109.       strcatbuff(save,a);
  1110.       */
  1111.  
  1112.       /* add name */
  1113.       ADD_STANDARD_NAME(0);
  1114.             }
  1115.       break;
  1116.     }
  1117.  
  1118.     hts_lowcase(save);
  1119.  
  1120.     if (save[strlen(save)-1]=='/') 
  1121.       strcatbuff(save,DEFAULT_HTML);     // nommer page par dΘfaut!!
  1122.   }
  1123.  
  1124.  
  1125.   // vΘrifier qu'on ne doit pas forcer l'extension
  1126.   // par exemple, asp sera sauvΘ en html, cgi en html ou gif, xbm etc.. selon les cas
  1127.   /*if (ext_chg) {
  1128.     char* a=save+strlen(save)-1;
  1129.     while(((int) a>(int) save) && (*a!='.') && (*a!='/')) a--;
  1130.     if (*a=='.') *a='\0';  // couper
  1131.     // recopier extension
  1132.     strcatbuff(save,".");
  1133.     strcatbuff(save,ext);    // copier ext
  1134.   }*/
  1135.   // de mΩme en cas de manque d'extension on en place une de maniΦre forcΘe..
  1136.   // cela Θvite les /chez/toto et les /chez/toto/index.html incompatibles
  1137.   if (opt->savename_type != -1) {
  1138.     char* a=save+strlen(save)-1;
  1139.     while(( a > save) && (*a!='.') && (*a!='/')) a--;
  1140.     if (*a!='.') {   // agh pas de point
  1141.       //strcatbuff(save,".none");                 // a Θviter
  1142.       strcatbuff(save,".html");                   // prΘfΘrable!
  1143.       if ( (opt->debug>1) && (opt->errlog!=NULL) ) {
  1144.         fspc(opt->errlog,"warning"); fprintf(opt->errlog,"Default HTML type set for %s%s"LF,adr_complete,fil_complete);
  1145.         test_flush;
  1146.       }
  1147.     }
  1148.   }
  1149.  
  1150.   // effacer pass au besoin pour les autentifications
  1151.   // (plus la peine : masquΘ au dΘbut)
  1152. /*
  1153.   {
  1154.     char* a=jump_identification(save);
  1155.     if (a!=save) {
  1156.       char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1157.       char *b;
  1158.       tempo[0]='\0';
  1159.       strcpybuff(tempo,"[");
  1160.       b=strchr(save,':');
  1161.       if (!b) b=strchr(save,'@');
  1162.       if (b)
  1163.         strncatbuff(tempo,save,(int) b-(int) a);
  1164.       strcatbuff(tempo,"]");
  1165.       strcatbuff(tempo,a);
  1166.       strcpybuff(save,a);
  1167.     }
  1168.   }
  1169. */
  1170.  
  1171.   // Θviter les / au dΘbut (cause: N100)
  1172.   if (save[0]=='/') {
  1173.     char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1174.     strcpybuff(tempo,save+1);
  1175.     strcpybuff(save,tempo);
  1176.   }
  1177.  
  1178.   // changer les ~,:,",*,? en _ pour sauver sur disque
  1179.   hts_replace(save,'~','_');  // interdit sous unix (~foo)
  1180.   //
  1181.   hts_replace(save,'\\','_');
  1182.   hts_replace(save,':','_');  // interdit sous windows
  1183.   hts_replace(save,'*','_');  // interdit sous windows
  1184.   hts_replace(save,'?','_');  // doit pas arriver!!
  1185.   hts_replace(save,'\"','_');  // interdit sous windows
  1186.   hts_replace(save,'<','_');  // interdit sous windows
  1187.   hts_replace(save,'>','_');  // interdit sous windows
  1188.   hts_replace(save,'|','_');  // interdit sous windows
  1189.   //
  1190.   hts_replace(save,'@','_');
  1191.   if (opt->savename_83 == 2) { // CDROM
  1192.     // maybe other ones?
  1193.     hts_replace(save,'-','_');
  1194.     hts_replace(save,'=','_');
  1195.     hts_replace(save,'+','_');
  1196.   }
  1197.   //
  1198.   { // Θliminer les // (comme ftp://)
  1199.     char* a;
  1200.     while( (a=strstr(save,"//")) ) *a='_';
  1201.     // Eliminer chars spΘciaux
  1202.     a=save -1 ;
  1203.     while(*(++a))
  1204.       if ( ((unsigned char)(*a) <= 31)
  1205.           || ((unsigned char)(*a) == 127) )
  1206.         *a='_';
  1207.   }
  1208.  
  1209.    
  1210. #if HTS_OVERRIDE_DOS_FOLDERS
  1211.   /* Replace /foo/nul/bar by /foo/nul_/bar */
  1212.   {
  1213.     int i=0;
  1214.     while(hts_tbdev[i][0]) {
  1215.       char* a=save;
  1216.       while((a=strstrcase(a,(char*)hts_tbdev[i]))) {
  1217.         switch ( (int) a[strlen(hts_tbdev[i])] ) {
  1218.         case '\0':
  1219.         case '/':
  1220.         case '.': 
  1221.         {
  1222.           char BIGSTK tempo[HTS_URLMAXSIZE*2]; tempo[0]='\0';
  1223.           strncatbuff(tempo,save,(int) (a - save) + strlen(hts_tbdev[i]));
  1224.           strcatbuff(tempo,"_");
  1225.           strcatbuff(tempo,a+strlen(hts_tbdev[i]));
  1226.           strcpybuff(save,tempo);
  1227.                    }
  1228.           break;
  1229.         }
  1230.         a+=strlen(hts_tbdev[i]);
  1231.       }
  1232.       i++;
  1233.     }
  1234.   }
  1235.   /* Strip ending . or ' ' forbidden on windoz */
  1236.   {
  1237.     int len;
  1238.     char* a=save;
  1239.     while((a=strstr(a,"./"))) {
  1240.       *a = '_';
  1241.     }
  1242.     a=save;
  1243.     while((a=strstr(a," /"))) {
  1244.       *a = '_';
  1245.     }
  1246.     len = (int) strlen(save);
  1247.     if (len > 0 && ( save[len - 1] == '.' || save[len - 1] == ' ') ) {
  1248.       save[len - 1] = '_';
  1249.     }
  1250.   }
  1251. #endif
  1252.  
  1253.   // conversion 8-3 .. y compris pour les rΘpertoires
  1254.   if (opt->savename_83) {
  1255.     char BIGSTK n83[HTS_URLMAXSIZE*2];
  1256.     long_to_83(opt->savename_83,n83,save);
  1257.     strcpybuff(save,n83);
  1258.   }
  1259.  
  1260.   // enforce stricter ISO9660 compliance (bug reported by Steffo Carlsson)
  1261.   // Level 1 File names are restricted to 8 characters with a 3 character extension, 
  1262.   // upper case letters, numbers and underscore; maximum depth of directories is 8.
  1263.   // This will be our "DOS mode"
  1264.   // L2: 31 characters
  1265.   // A-Z,0-9,_
  1266.   if (opt->savename_83 > 0) {
  1267.     char *a, *last;
  1268.     for(last = save + strlen(save) - 1 ; last != save && *last != '/' && *last != '\\' && *last != '.' ; last--);
  1269.     if (*last != '.') {
  1270.       last = NULL;
  1271.     }
  1272.     for(a = save ; *a != '\0' ; a++) {
  1273.       if (*a >= 'a' && *a <= 'z') {
  1274.         *a -= 'a' - 'A';
  1275.       }
  1276.       else if (*a == '.') {
  1277.         if (a != last) {
  1278.           *a = '_';
  1279.         }
  1280.       }
  1281.       else if ( ! ( (*a >= 'A' && *a <= 'Z') || (*a >= '0' && *a <= '9') || *a == '_' || *a == '/' || *a == '\\') ) {
  1282.         *a = '_';
  1283.       }
  1284.     }
  1285.   }
  1286.  
  1287.   /* ensure that there is no ../ (potential vulnerability) */
  1288.   fil_simplifie(save);
  1289.  
  1290. #if HTS_ANALYSTE
  1291.   {
  1292.     hts_htmlcheck_savename(adr_complete,fil_complete,referer_adr,referer_fil,save);
  1293.     if ( (opt->debug>0) && (opt->log!=NULL) ) {
  1294.       fspc(opt->log,"info"); fprintf(opt->log,"engine: save-name: local name: %s%s -> %s"LF,adr,fil,save);
  1295.       test_flush;
  1296.     }
  1297.   }
  1298. #endif
  1299.  
  1300.   // chemin primaire Θventuel A METTRE AVANT
  1301.   if (strnotempty(opt->path_html)) {
  1302.     char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1303.     strcpybuff(tempo,opt->path_html);
  1304.     strcatbuff(tempo,save);
  1305.     strcpybuff(save,tempo);
  1306.   }
  1307.  
  1308.  
  1309.   // vΘrifier que le nom n'est pas dΘja pris...
  1310.   if (liens!=NULL) { 
  1311.     int nom_ok;
  1312.     do {
  1313.       int i;
  1314.       int len;
  1315.       len=strlen(save);    // taille
  1316.       //
  1317.       nom_ok=1;  // α priori bon
  1318.       // on part de la fin pour optimiser, plus les opti de taille pour aller encore plus vite..
  1319. #if DEBUG_SAVENAME
  1320. printf("\nStart search\n");
  1321. #endif
  1322.  
  1323.       i=hash_read(hash,save,"",0,0);      // lecture type 0 (sav)
  1324.       if (i>=0)
  1325.           {
  1326.             int sameAdr = ( strfield2(liens[i]->adr, normadr) != 0 );
  1327.             int sameFil;
  1328.                         // NO - URL hack is only for stripping // and www.
  1329.             //if (opt->urlhack != 0)
  1330.             //  sameFil = ( strfield2(liens[i]->fil, normfil) != 0);
  1331.             //else
  1332.             sameFil = ( strcmp(liens[i]->fil, normfil) == 0);
  1333.             if (sameAdr && sameFil)
  1334.             {    // ok c'est le mΩme lien, adresse dΘja dΘfinie
  1335.               /* Take the existing name not to screw up with cAsE sEnSiTiViTy of Linux/Unix */
  1336.               if (strcmp(liens[i]->sav, save) != 0) {
  1337.                 strcpybuff(save, liens[i]->sav);
  1338.               }
  1339.               i=0;
  1340. #if DEBUG_SAVENAME
  1341. printf("\nOK ALREADY DEFINED\n",13,i);
  1342. #endif
  1343.             } else {  // utilisΘ par un AUTRE, changer de nom
  1344.               char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1345.               char* a=save+strlen(save)-1;
  1346.               char* b;
  1347.               int n=2;       
  1348.               char collisionSeparator = ( (opt->savename_83 != 2) ? '-' : '_');
  1349.               tempo[0]='\0';
  1350.  
  1351. #if DEBUG_SAVENAME
  1352. printf("\nWRONG CASE UNMATCH : \n%s\n%s, REDEFINE\n",liens[i]->fil,fil_complete);
  1353. #endif
  1354.               nom_ok=0;
  1355.               i=0;
  1356.               
  1357.               while(( a > save) && (*a!='.') && (*a!='\\') && (*a!='/')) a--;
  1358.               if (*a=='.')
  1359.                 strncatbuff(tempo,save,(int) (a - save));
  1360.               else
  1361.                 strcatbuff(tempo,save);
  1362.               
  1363.               // tester la prΘsence d'un -xx (ex: index-2.html -> index-3.html)
  1364.               b=tempo+strlen(tempo)-1;
  1365.               while (isdigit((unsigned char)*b)) b--;
  1366.               if (*b == collisionSeparator) {
  1367.                 sscanf(b+1,"%d",&n);
  1368.                 *b='\0';    // couper
  1369.                 n++;  // plus un
  1370.               }
  1371.               
  1372.               // en plus il faut gΘrer le 8-3 .. pas facile le client
  1373.               if (opt->savename_83) {
  1374.                 int max;
  1375.                 char* a=tempo+strlen(tempo)-1;
  1376.                 while(( a > tempo) && (*a!='/')) a--;
  1377.                 if (*a=='/') a++;
  1378.                 max=max_char-1-nombre_digit(n);
  1379.                 if ((int) strlen(a)>max)
  1380.                   *(a+max)='\0';  // couper sinon il n'y aura pas la place!
  1381.               }
  1382.               
  1383.               // ajouter -xx (ex: index.html -> index-2.html)
  1384.               sprintf(tempo+strlen(tempo),"%c%d", collisionSeparator, n);
  1385.               
  1386.               // ajouter extension
  1387.               if (*a=='.')
  1388.                 strcatbuff(tempo,a);
  1389.               
  1390.               strcpybuff(save,tempo);
  1391.               
  1392.               //printf("switched: %s\n",save);
  1393.               
  1394.             }  // if
  1395.           }
  1396. #if DEBUG_SAVENAME
  1397. printf("\nEnd search, %s\n",fil_complete);
  1398. #endif
  1399.     } while(!nom_ok);
  1400.     
  1401.   }
  1402.   
  1403.   //printf("'%s' %s %s\n",save,adr,fil);
  1404.       
  1405.   return 0;
  1406. }
  1407.  
  1408. /* nom avec md5 urilisΘ partout */
  1409. void standard_name(char* b,char* dot_pos,char* nom_pos,char* fil_complete,int short_ver) {
  1410.   b[0]='\0';
  1411.   /* Nom */
  1412.   if (dot_pos) {
  1413.     if (!short_ver)    // Noms longs
  1414.       strncatbuff(b,nom_pos,(int) (dot_pos - nom_pos));
  1415.     else
  1416.       strncatbuff(b,nom_pos,min((int) (dot_pos - nom_pos),8));
  1417.   } else {
  1418.     if (!short_ver)    // Noms longs
  1419.       strcatbuff(b,nom_pos);
  1420.     else
  1421.       strncatbuff(b,nom_pos,8);
  1422.   }
  1423.   /* MD5 - 16 bits */
  1424.   strncatbuff(b,url_md5(fil_complete),4);
  1425.   /* Ext */
  1426.   if (dot_pos) {
  1427.     strcatbuff(b,".");
  1428.     if (!short_ver)    // Noms longs
  1429.       strcatbuff(b,dot_pos+1);
  1430.     else
  1431.       strncatbuff(b,dot_pos+1,3);
  1432.   } else {
  1433.     if (!short_ver)    // Noms longs
  1434.       strcatbuff(b,DEFAULT_EXT + 1);    // pas de..
  1435.     else
  1436.       strcatbuff(b,DEFAULT_EXT_SHORT + 1);    // pas de..
  1437.   }
  1438. }
  1439.  
  1440.  
  1441. /* Petit md5 */
  1442. char* url_md5(char* fil_complete) {
  1443.   char* digest;
  1444.   char* a;
  1445.   NOSTATIC_RESERVE(digest, char, 32+2);
  1446.   digest[0]='\0';
  1447.   a=strchr(fil_complete,'?');
  1448.   if (a) {
  1449.     if (strlen(a)) {
  1450.       char BIGSTK buff[HTS_URLMAXSIZE*2];
  1451.       a++;
  1452.       digest[0]=buff[0]='\0';
  1453.       strcatbuff(buff,a);         /* query string MD5 */
  1454.       domd5mem(buff,strlen(buff),digest,1);
  1455.     }
  1456.   }
  1457.   return digest;
  1458. }
  1459.  
  1460. // interne α url_savename: ajoute une chaεne α une autre avec \ -> /
  1461. void url_savename_addstr(char* d,char* s) {
  1462.   int i=strlen(d);
  1463.   while(*s) {
  1464.     if (*s=='\\')  // remplacer \ par des /
  1465.       d[i++]='/';
  1466.     else
  1467.       d[i++]=*s;
  1468.     s++;
  1469.   }
  1470.   d[i]='\0';
  1471. }
  1472.  
  1473. #undef test_flush
  1474.